/****
 * AOC starter
 * Copyright (c) 2010, 02nz
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Latest version aviliable through the world-wide-web at the following url:
 * http://code.google.com/p/aoc-starter/
 ****/

#include <windows.h>
#include "_patcher.h"

namespace patcher
{

inline bool DataCompare(PUCHAR pData, PUCHAR pMask, LPCSTR szMask)
{
	for( ; *szMask; ++szMask, ++pData, ++pMask )
		if(*szMask != '?' && *pData != *pMask )
			return false;

	return (*szMask == 0);
}

PUCHAR FindPattern(PUCHAR Start, PUCHAR End, LPCSTR pbMask, LPCSTR szMask)
{
	ULONG Len = lstrlenA(szMask);

	while(Start < End - Len)
	{
		if(DataCompare(Start, (PUCHAR)pbMask, szMask))
			return Start;

		Start++;
	}

	return 0;
}

PUCHAR FindPatternBack(PUCHAR From, PUCHAR To, LPCSTR pbMask, LPCSTR szMask)
{
	while(From >= To)
	{
		if(DataCompare(From, (PUCHAR)pbMask, szMask))
			return From;

		From--;
	}

	return 0;
}

PUCHAR FindStringPtr(PUCHAR Start, PUCHAR End, LPCSTR Str, bool FullString)
{
	ULONG Len = (FullString ? 1 : 0) + lstrlenA(Str); // +null char

	while(Start < End - Len)
	{
		if(!memcmp(Start, Str, Len))
			return Start;

		Start++;
	}

	return NULL;
}

PUCHAR FindOpPtr(PUCHAR Start, PUCHAR End, UCHAR Op, PUCHAR Ptr)
{
	UCHAR SIG[5];

	SIG[0] = Op;
	*(PUCHAR *)&SIG[1] = Ptr;

	while(Start < End - 5)
	{
		if(!memcmp(Start, &SIG, 5))
			return Start;

		Start++;
	}

	return NULL;
}

PUCHAR FindPushPtr(PUCHAR Start, PUCHAR End, PUCHAR Ptr)
{
	return FindOpPtr(Start, End, 0x68, Ptr);
}

PUCHAR FindEpilogFrom(PUCHAR From, ULONG Max)
{
// 55     push ebp
// 8B EC  mov  ebp, esp

	UCHAR SIG[] = { 0x55, 0x8B, 0xEC };

	while(Max)
	{
		if(!memcmp(From, &SIG, 3))
			return From;

		From--;
		Max--;
	}

	return NULL;
}

}

ULONG GPA(PVOID Base, PCSTR Func)
{
	PBYTE Lib = (PBYTE)Base;

	PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER) Lib;
	PIMAGE_NT_HEADERS pPe = (PIMAGE_NT_HEADERS) &Lib[ pDos->e_lfanew ];

	if(	pDos->e_magic == IMAGE_DOS_SIGNATURE &&
		pDos->e_lfanew &&
		pPe->Signature == IMAGE_NT_SIGNATURE &&
		pPe->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress )
	{
        PIMAGE_EXPORT_DIRECTORY	pExport = (PIMAGE_EXPORT_DIRECTORY) &Lib[ pPe->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress ];
        if(pExport->NumberOfFunctions)
		{
	        PULONG Names = (PULONG) &Lib[ pExport->AddressOfNames ];
			PUSHORT	Ordinals = (PUSHORT) &Lib[ pExport->AddressOfNameOrdinals ];

	        for(DWORD Name = 0; Name < pExport->NumberOfNames; Name++)
				if(!lstrcmpA((PSTR)&Lib[ Names[ Name ] ], Func))
				{
					PULONG Functions = (PULONG) &Lib[ pExport->AddressOfFunctions ];
					return Functions[ Ordinals[ Name ] ];
				}
		}
	}

	return 0;
}

